<?php

/********************************
 *                              *
 * 自动加载配置文件             *
 *                              *
 * ******************************
 */
use Phalcon\Loader;
use Phalcon\DiInterface;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Cache\Frontend\Data as FrontendData;
use Phalcon\Cache\Backend\File as BackFile;
use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Flash\Session as FlashSession;

// 创建对象
$loader = new Loader();

// 注册命名空间
$loader->registerNamespaces([
    'App\Frontend' => '../Application/' . APPS_DIR . '/Frontend/',

    //Library
    'App\Library' => '../Application/Librarys/',
    //'Phalcon' => '../Application/Librarys/Phalcon/',
    //Apis
    'Api' => "../Application/Apis/",
]);

$loader->register();

// 注入视图缓存服务
$di->set('viewCache', function () {
    // 默认缓存时间为一天
    $frontCache = new Phalcon\Cache\Frontend\Output();
    // 缓存目录
    $cache = new Phalcon\Cache\Backend\File($frontCache, array(
        "cacheDir" => "data/caches/html/"
    ));
    return $cache;
});

// 注入cookie服务
$di->set('cookies', function () {
    $cookies = new Phalcon\Http\Response\Cookies();
    $cookies->useEncryption(true);

    return $cookies;
});

// 注入session服务
$di->set('session', function () {
    $session = new Phalcon\Session\Adapter\Files();
    $session->start();
    return $session;
});

$di->set('crypt', function () {
    $crypt = new Phalcon\Crypt();

    $crypt->setKey('#1y9y9y0y-0m-7m-2d-6d'); // 使用你自己的key！

    return $crypt;
});

// 利用自定义的CSS类来注册flash服务
$di->set(
    "flash",
    function () {
        $flash = new FlashDirect(
            [
                "error"   => "my-alert-error hidden",
                "success" => "my-alert-success hidden",
                "notice"  => "my-alert-notice hidden",
                "warning" => "my-alert-warning hidden",
            ]
        );

        return $flash;
    }
);

// 利用自定义的CSS类来注册flashSession服务
$di->set(
    "flashSession",
    function () {
        $flash = new FlashSession(
            [
                "error"   => "my-alert-error hidden",
                "success" => "my-alert-success hidden",
                "notice"  => "my-alert-notice hidden",
                "warning" => "my-alert-warning hidden",
            ]
        );

        return $flash;
    }
);

// 设置模型缓存服务
$di->set(
    "cache",
    function () {
        // 默认缓存时间为一天
        $frontCache = new FrontendData(
            [
                "lifetime" => 86400,
            ]
        );
        $cache = new BackFile(
            $frontCache,
            [
                "cacheDir" => "../public/data/caches/file/",
            ]
        );

        return $cache;
    }
);